package cn.edu.scau.cmi.xianjiehua.comprehension.client;

import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Set;

import cn.edu.scau.cmi.xianjiehua.comprehension.dao.StudentDao;
import cn.edu.scau.cmi.xianjiehua.comprehension.dao.TutorDao;
import cn.edu.scau.cmi.xianjiehua.comprehension.domain.Student;
import cn.edu.scau.cmi.xianjiehua.comprehension.domain.Tutor;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class ViewController implements Initializable{
	
	@FXML
	private TextField studentId;
	
	@FXML
	private TextField studentName;
	
	@FXML
	private TextField attachTutor;
	
	@FXML
	private TextField tutorId;
	
	@FXML
	private TextField tutorName;
	
	@FXML
	private TextField modifyStudent;
	
	@FXML
	private TextArea console;
	
	@FXML
	private Button addStudent;

	@FXML
	private Button updateStudent;
	
	@FXML
	private Button findStudentById;
	
	@FXML
	private Button delStudent;
	
	@FXML
	private Button findAllTutors;
	
	@FXML
	private Button StudentAttachTutor;
	
	@FXML
	private Button StudentDisattachTutor;
	
	@FXML
	private Button addTutor;
	
	@FXML
	private Button TutorAttachStudent;
	
	@FXML
	private Button updateTutor;
	
	@FXML
	private Button delTutor;
	
	@FXML
	private Button TutorDisattachStudent;
	
	@FXML
	private Button findTutorById;
	
	@FXML
	private Button findAllStudentByTutor;
	

	StudentDao studentDao;
	
	TutorDao tutorDao;
	
	
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		// TODO Auto-generated method stub
		studentDao = new StudentDao();
		tutorDao = new TutorDao();
	}
	
	/**
	 * 添加学生
	 * @param event
	 */
	public void addStudent(ActionEvent event) {
		String stuName = studentName.getText();
		Student stu = new Student();
		stu.setName(stuName);
		studentDao.save(stu);
		
		console.appendText("学生添加成功：" + stu.toString()+"\n");
	}
	
	/**
	 * 删除学生
	 * @param event
	 */
	public void delStudentEvent(ActionEvent event) {
		Long id = Long.parseLong(studentId.getText());
		
		Student stu = studentDao.findById(id);
		if(stu != null) {
			studentDao.delete(stu);
			console.appendText("学生删除成功：" + stu.toString()+" \n");
		}else {
			console.appendText("学生不存在\n");
		}
	}
	
	/**
	 * 更新学生
	 * @param event
	 */
	public void updateStudentEvent(ActionEvent event) {
		Long id = Long.parseLong(studentId.getText());
		String newName = studentName.getText();
		Student stu = studentDao.findById(id);
		if(stu != null) {
			stu.setName(newName);
			stu = studentDao.merge(stu);
			console.appendText("学生更新成功：" + stu.toString()+"\n");
		}else {
			console.appendText("学生不存在\n");
		}
	}
	
	/**
	 * 根据id查看学生
	 */
	public void findStudentById(ActionEvent event) {
		Long id = Long.parseLong(studentId.getText());
		Student stu = studentDao.findById(id);
		if(stu != null) {
			console.appendText("学生信息：" + stu.toString()+"\n");
		}else {
			console.appendText("学生不存在\n");
		}
	}
	
	/**
	 * 添加教师
	 * @param event
	 */
	public void addTutorEvent(ActionEvent event) {
		String name = tutorName.getText();
		Tutor tutor = new Tutor();
		tutor.setName(name);
		tutorDao.save(tutor);;
		
		console.appendText("教师添加成功：" + tutor.toString()+"\n");
	}

	/**
	 * 删除教师
	 * @param event
	 */
	public void delTutorEvent(ActionEvent event) {
		Long id = Long.parseLong(tutorId.getText());
		Tutor tutor = tutorDao.findById(id);
		if(tutor != null) {
			tutor.setStudents(null); //消除关联
			tutorDao.delete(tutor);
			console.appendText("教师删除成功：" + tutor.toString()+" \n");
		}else {
			console.appendText("教师不存在\n");
		}
	}
	
	/**
	 * 更新教师
	 * @param event
	 */
	public void updateTutorEvent(ActionEvent event) {
		Long id = Long.parseLong(tutorId.getText());
		String newName = tutorName.getText();
		Tutor tutor = tutorDao.findById(id);
		if(tutor != null) {
			tutor.setName(newName);
			tutor = tutorDao.merge(tutor);
			console.appendText("教师更新成功：" + tutor.toString()+"\n");
		}else {
			console.appendText("教师不存在\n");
		}
	}
	
	/**
	 * 根据id查看教师
	 */
	public void findTutorById(ActionEvent event) {
		Long id = Long.parseLong(tutorId.getText());
		Tutor tutor = tutorDao.findById(id);
		if(tutor != null) {
			console.appendText("教师信息：" + tutor.toString()+"\n");
			tutor.getStudents().forEach(e -> console.appendText("指导的学生信息：" + e.toString()+"\n"));
		}else {
			console.appendText("教师不存在\n");
		}
	}
	
	/**
	 * 查看教师可以绑定的学生
	 * @param event
	 */
	public void  showAllStudentsByTutorId(ActionEvent event) {
		studentDao.findAll().forEach(e ->{
			if(e.getTutor() == null)
				console.appendText("可指导的学生信息：" + e.toString()+"\n");
		});
	}
	
	/**
	 * 查看学生可绑定的教师
	 * @param event
	 */
	public void showAllTutors(ActionEvent event) {
		tutorDao.findAll().forEach(e -> console.appendText("可选择的指导教师：" + e.toString()+"\n"));
	}
	
	/**
	 * 学生绑定教师
	 * @param event
	 */
	public void attachTutor(ActionEvent event) {
		Long tid = Long.parseLong(attachTutor.getText());
		Long sid = Long.parseLong(studentId.getText());
		
		Student stu = studentDao.findById(sid);
		if(stu == null) {
			console.appendText("学生不存在\n");
			return;
		}
		Tutor tutor = tutorDao.findById(tid);
		if(tutor == null) {
			console.appendText("教师不存在\n");
			return;
		}
		stu.setTutor(tutor);
		studentDao.attachDirty(stu);
		console.appendText("学生 " + stu.toString()+" 绑定教师 "+tutor+" 成功\n");
	}
	
	/**
	 * 学生取消绑定教师
	 * @param event
	 */
	public void disattachTutor(ActionEvent event) {
		Long sid = Long.parseLong(studentId.getText());
		
		Student stu = studentDao.findById(sid);
		if(stu == null) {
			console.appendText("学生不存在\n");
			return;
		}
		Tutor tutor = stu.getTutor();
		stu.setTutor(null);
		if(tutor == null) {
			console.appendText("没有指导教师\n");
			return;
		}
		studentDao.attachDirty(stu);
		console.appendText("学生 " + stu.toString()+" 取消绑定教师 "+tutor+" 成功\n");
	}
	
	/**
	 * 教师绑定学生
	 * @param event
	 */
	public void attachStudent(ActionEvent event) {
		Long tid = Long.parseLong(tutorId.getText());
		Long sid = Long.parseLong(modifyStudent.getText());
		
		Student stu = studentDao.findById(sid);
		if(stu == null) {
			console.appendText("学生不存在\n");
			return;
		}else if(stu.getTutor() != null) {
			console.appendText("学生已有指导教师\n");
			return;
		}
		Tutor tutor = tutorDao.findById(tid);
		if(tutor == null) {
			console.appendText("教师不存在\n");
			return;
		}
		stu.setTutor(tutor);
		studentDao.attachDirty(stu);
		console.appendText("教师 " + tutor+" 绑定学生 "+stu.toString()+" 成功\n");
	}
	
	/**
	 * 教师取消绑定学生
	 * @param event
	 */
	public void disattachStudent(ActionEvent event) {
		Long tid = Long.parseLong(tutorId.getText());
		Long sid = Long.parseLong(modifyStudent.getText());
		
		Student stu = studentDao.findById(sid);
		if(stu == null) {
			console.appendText("学生不存在\n");
			return;
		}
		Tutor tutor = stu.getTutor();
		stu.setTutor(null);
		if(tutor == null) {
			console.appendText("教师不存在\n");
		}else if(tutor.getId() != tid) {
			console.appendText("您不是 " + tutor +" 无法对学生 "+ stu +"操作\n");
		}
		studentDao.attachDirty(stu);
		console.appendText("教师 " + tutor+" 取消绑定学生 "+stu.toString()+" 成功\n");
	}
}
